﻿Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.ComponentModel


Public Class Main
    Public WithEvents oWindowList As OpenedWindowList = OpenedWindowList.GetList()
    Private WithEvents oWindow As OpenedWindowClass
    Public Shared v_username As String


#Region "Support Procedures"

    Private Sub HookupChangeEventHandlers()
        ' Hook up change notification. This really
        ' isn't part of the ObservableCollection class,
        ' but it seems that you would want to know
        ' how to track modifications in the 
        ' properties of the children of the collection:
        For Each oWindow As OpenedWindowClass In oWindowList
            HookupChangeEventHandler(oWindow)
        Next
    End Sub

    Private Sub HookupChangeEventHandler(ByVal oWindow As OpenedWindowClass)
        ' Add a PropertyChanged event handler for 
        ' the specified Customer instance:
        AddHandler oWindow.PropertyChanged, _
          AddressOf HandlePropertyChanged
    End Sub

    Private Sub SetupData()
        HookupChangeEventHandlers()
    End Sub

    Private Sub RibbonButtonExpense_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        For itemIndex = 0 To Application.Current.Windows.Count - 1
            If Application.Current.Windows(itemIndex).Name = sender.tag Then
                Application.Current.Windows(itemIndex).Activate()
            End If
        Next
    End Sub

    Private Sub addToOWindowList(windowId As String, windowName As String, windowTitle As String, windowImage As String)
        oWindow = New OpenedWindowClass(windowId, windowName, windowTitle, windowImage, False)
        HookupChangeEventHandler(oWindow)
        oWindowList.Add(oWindow)
    End Sub
#End Region

#Region "Change Event Handlers"

    Private Sub HandlePropertyChanged( _
    ByVal sender As Object, _
    ByVal e As PropertyChangedEventArgs)

        Dim propName As String = e.PropertyName
        Dim myWindow As OpenedWindowClass = CType(sender, OpenedWindowClass)


        Dim propInfo As System.Reflection.PropertyInfo = _
          GetType(OpenedWindowClass).GetProperty(propName)
        Dim value As Object = _
          propInfo.GetValue(myWindow, Nothing)
    End Sub

    Private Sub Data_CollectionChanged( _
      ByVal sender As Object, _
      ByVal e As NotifyCollectionChangedEventArgs) _
      Handles oWindowList.CollectionChanged

        If e.Action = NotifyCollectionChangedAction.Add Or _
          e.Action = NotifyCollectionChangedAction.Remove Then

        End If
    End Sub

#End Region

#Region "Control Events"
    Private Sub ButtonAssets_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ButtonAssets.Click
        Dim nWindow As New AssetWindow
        nWindow.Title = "Asset"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/items.png")

    End Sub

    Private Sub ButtonCategories_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles ButtonCategories.Click
        Dim nWindow As New CategoryWindow
        nWindow.Title = "Category"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/clipboard-10570.png")
    End Sub

    Private Sub ButtonLocations_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles ButtonLocations.Click
        Dim nWindow As New LocationWindow
        nWindow.Title = "Location"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/location.png")
    End Sub

    Private Sub ButtonPersons_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles ButtonPersons.Click
        Dim nWindow As New PersonWindow
        nWindow.Title = "Person"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/personal.png")
    End Sub
    Private Sub ButtonDepartments_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles ButtonDepartments.Click
        Dim nWindow As New DepartmentWindow
        nWindow.Title = "Department"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/Office-Pencil-Pot-64.png")
    End Sub

    Private Sub ButtonSynch_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles ButtonSynch.Click
        Dim nWindow As New ReaderWindow
        nWindow.Title = "Reader"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/connectestablished-35258.png")
    End Sub

    Private Sub ButtonLedgerClassification_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles ButtonLedgerClassification.Click
        Dim nWindow As New LedgerClassificationWindow
        nWindow.Title = "Ledger - Classification"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/assetclassification.png")
    End Sub

    Private Sub ButtonLedgerDepartment_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles ButtonLedgerDepartment.Click
        Dim nWindow As New LedgerClassificationWindow
        nWindow.Title = "Ledger - Department"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/Office-Pencil-Pot-64.png")
    End Sub

    Private Sub ButtonLedgerLeased_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles ButtonLedgerLeased.Click
        Dim nWindow As New LedgerClassificationWindow
        nWindow.Title = "Ledger - Leased Assets"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/leasedasset.png")
    End Sub

    Private Sub ButtonLedgerLocation_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles ButtonLedgerLocation.Click
        Dim nWindow As New LedgerLocationWindow
        nWindow.Title = "Ledger - Location"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/location.png")
    End Sub

    Private Sub ButtonReports_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles ButtonReports.Click, ButtonQATReports.Click
        Dim nWindow As New ReportWindow
        nWindow.Title = "Report"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/printer-64.png")
    End Sub

    Private Sub buttonAccountPolicies_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles buttonAccountPolicies.Click
        Dim nWindow As New AccountPolicies
        nWindow.Title = "Account Policies"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/settings-3597.png")
    End Sub
    Private Sub buttonReaderSettings_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles buttonReaderSettings.Click
        Dim nWindow As New ReaderSettings
        nWindow.Title = "Reader Settings"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/settings-2770.png")
    End Sub

    Private Sub buttonAccounts_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles buttonAccounts.Click
        Dim nWindow As New UserAdministration
        nWindow.Title = "User Administration"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/contact.png")
    End Sub

    Private Sub buttonAdminReports_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles buttonAdminReports.Click
        Dim nWindow As New ReportSettings
        nWindow.Title = "Report Settings"
        nWindow.Name = "window" & oWindowList.Count
        nWindow.Show()

        addToOWindowList(nWindow.Name, nWindow.Title, nWindow.Title, "../../images/main/printer-64.png")
    End Sub

    Private Sub RibbonButtonActiveWindow_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        For itemIndex = 0 To Application.Current.Windows.Count - 1
            If Application.Current.Windows(itemIndex).Name = sender.tag Then
                Application.Current.Windows(itemIndex).Activate()
            End If
        Next
    End Sub



#End Region

#Region "Window Events"
    Private Sub Main_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        Application.Current.Shutdown()
    End Sub

    Private Sub Main_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        Dim v_isDirty As Boolean = False
        For Each item In oWindowList
            If item.WindowIsDirty Then
                 MessageBox.Show("There are unsaved items, close or save the window before closing ATS.", "ATS", MessageBoxButton.OK, MessageBoxImage.Information)
                e.Cancel = True
            End If
        Next
    End Sub

    Private Sub Main_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        labelUser.Content = Environment.UserName
    End Sub
#End Region
    
    
    
End Class

